/**
 * v 1.1 [02.06.2009]
 *
 * Event Table 
 *
 * request:
 * - mootools 1.2
 *
 **/


var EventTable = new Class({
	
	Implements: [Events, Options],
	
	options:{
		'mode' 			: 'script',
		'maxRows'		: 10,
		'cssTable'		: 'event-table',
		'cssHighlight'	: 'highlight',
		'cssMouseEnter'	: 'mo',
		'cellpadding' 	: '0',
		'cellspacing' 	: '0'
	},
	
	initialize : function(element, options){
		
		this.element = $(element);
		
		this.setOptions(options);
		
		this.iterator = 0;
		
		this.table = new Element('table',{
			'class': this.options.cssTable,
			'cellpadding': this.options.cellpadding,
			'cellspacing': this.options.cellspacing,
			'border': '0'
		});
		this.tbody = new Element('tbody');
		
		this.tr = new Element('tr');
		
		this.td = new Element('td');
		
		//this.addHead();
	},
	/*
	'addHead' : function(){
		
		var row = this.tr.clone();
		var th = new Element('th',{
			'html':'123',
			'styles':{
				'width': '100px'
			}
		}).inject(row);
		
		new Element('span', {
			'styles':{
				'border':'1px solid #00ff00',
				'float':'right',
				'height':'16px',
				'cursor':'w-resize'
			},
			'events':{
				'mousedown':function(ev){
					
				},
				'mouseup':function(ev){
					alert(123);
				}
			}
		}).inject(th);
		
		this.table.grab(row);
	},
	*/
	addRow: function(dataArray, id){
		
		this.fireEvent('onAddRow', [dataArray, id]);
		
		var row = this.tr.clone(false);
		
		var id = id || this.iterator;
		
		row.set('id', id);
		
		var _class = this.iterator % 2 ? 'even' : 'odd';
		row.addClass(_class);
		
		this.setEvents(row);
		
		if ($type(dataArray) == 'object') dataArray = $H(dataArray);
		
		dataArray.each(function(el){
			
			if ($type(el) == 'element'){
				
				el.inject(this.td.clone(false).inject(row));
				
			}else{
				
				this.td.clone(false).set('html', el).inject(row);
				
			}
			
			
			
		}.bind(this));
		
		this.tbody.grab(row);
		
		this.iterator++;
	},
	
	setEvents: function(el){
		
		var options = this.options;
		
		el.addEvents({
			'mouseenter' : function(){
				//if(!tr.hasClass(options.cssHighlight))
				el.addClass(options.cssMouseEnter);
				
				this.fireEvent('mouseenter', [el]);
				
			}.bind(this),
			'mouseleave' : function(){
				el.removeClass(options.cssMouseEnter);
				
				this.fireEvent('mouseleave', [el]);
				
			}.bind(this),
			'click' : function(ev){
				
				this.fireEvent('click', [el.get('id'), el]);
			}.bind(this)
		});
		
	},
	
	load: function(obj){
		
		this.obj = obj;
		
		
		$this = this;
		
		if($type(obj) == 'string'){
			
			new Request.JSON({
				'url': obj,
				'onComplete': function(json_obj, html){
					
					if (!json_obj && _DEBUG_MODE) alert(html);
					
					switch($type(json_obj)){
						case 'object':
								
						
							$this.clear();
						
							$H(json_obj.rows).each(function(el, k){
								$this.addRow(el, k);
							});
							
							break;
						case 2:
							break;
						case 3:
							break;
						default:
							
					}
					
					$this.write();
				}
			}).get({'action':'list', 'maxRows':this.options.maxRows});
		}
	},
	
	reload: function(){
		this.load(this.obj);
	},
	
	clear: function(){
		this.table.empty();
	},
	
	write : function (element){
		/*
		if(!$chk(this.element)){
			this.element = $(element);
		}*/
		
		this.tbody.inject(this.table);
		
		this.table.inject(this.element);
	}
	
});